package com.jlz.managers.def;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Service;

import com.jlz.beans.def.ColumnBean;
import com.julewa.db.Store;

@Service
public class Columns {
	
	
	private Store<ColumnBean> store = new Store<ColumnBean>(ColumnBean.class);
	//
	public ColumnBean get(long colid){
		return store.get(colid);
	}
	//

	public ColumnBean create(String code){
		ColumnBean col = store.create();
		col.setCode(code);
		col.setLabel(code);
		col.setVisibleType(Columns.VISIBLE.DISPLAY.getValue());
		col.setEditType(Columns.EDITABLE.MUST.getValue());
		col.setValuetypeId(-1);//a node only
		return col;
	}
	
	//
	public ColumnBean create(){
		return store.create();
	}
	//
	public boolean insert(ColumnBean col){ 
		col.setID(store.generateID());
		return store.insert(col);
	} 
	public boolean update(ColumnBean col){
		return store.update(col);
	}
	public boolean remove(ColumnBean col){
		if(!col.getChildren().isEmpty())return false;
		return store.delete(col);
	}
	
	public List<ColumnBean> getColumnsOf(ColumnBean parent) {
		List<ColumnBean> ls = store.select("subColumnsOf", parent.getID());
		Collections.sort(ls);
		return ls;
	}
	
	public enum VISIBLE{
		HIDDEN(0),
		LIST(1),
		DISPLAY(2),
		EDIT(3)
		;int value;
		VISIBLE(int v){value = v;}
		public String getCode(){return name();}
		public int getValue(){return value;}
		public static final VISIBLE of(int v){
			for(VISIBLE dt:values()){
				if(dt.value == v)return dt;
			}
			return null;
		}
	};
	
	public VISIBLE[] getVisibleTypes(){
		return VISIBLE.values();
	}
	
	public enum EDITABLE{
		DISABLE(0),
		ENABLE(1),
		MUST(2)
		;int value;
		EDITABLE(int v){value = v;}
		public String getCode(){return name();}
		public int getValue(){return value;}
		public static final EDITABLE of(int v){
			for(EDITABLE dt:values()){
				if(dt.value == v)return dt;
			}
			return null;
		}
	};
	
	public EDITABLE[] getEditTypes(){
		return EDITABLE.values();
	}
	
}
